package com.logic.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.uhf.uhf.R
import kotlinx.android.synthetic.main.view_bottom_selector_content_item_layout.view.*

/**
 * @author xianpeng.sun
 * @date 2022/7/28 9:58
 * @Description:
 */
class BottomSelectorDialog constructor(context: Context) : Dialog(context, R.style.dialogStyle) {

    companion object {
        fun <T> arrayToList(array: Array<T>): MutableList<T> {
            return array.toMutableList()
        }
    }

    private var dialogTitle: TextView? = null
    private var dialogClose: ImageView? = null
    private var dialogRecycler: RecyclerView? = null
    private var adapter: BottomRecyclerAdapter? = null

    var selectorDatas: MutableList<String> = mutableListOf()
    var titleValue: String? = null
    var selectorPosition: Int = 0

    private var onSelectorClickListener: ((value: String, position: Int) -> Unit)? = null

    fun setOnSelectorClickListener(listener: (value: String, position: Int) -> Unit) {
        onSelectorClickListener = listener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.view_bottom_selector_dialog_layout)

        dialogTitle = findViewById(R.id.bottom_dialog_title)
        dialogClose = findViewById(R.id.bottom_dialog_close)
        dialogRecycler = findViewById(R.id.bottom_dialog_recycler)

        adapter = BottomRecyclerAdapter()
        dialogRecycler?.layoutManager = LinearLayoutManager(context)
        dialogRecycler?.adapter = adapter
        adapter?.selectorDatas = selectorDatas
        adapter?.selectorPosition = selectorPosition

        dialogTitle?.text = titleValue

        adapter?.setOnItemClickListener { _, _, position ->
            val item = selectorDatas[position]
            onSelectorClickListener?.invoke(item, position)
            dismiss()
        }

        dialogClose?.setOnClickListener {
            dismiss()
        }
    }

    override fun show() {
        super.show()

        val width = context.resources?.displayMetrics?.widthPixels
        val height = (context.resources?.displayMetrics?.heightPixels ?: 0) * 0.6

        val attributes = window?.attributes
        attributes?.width = width
        attributes?.height = height.toInt()
        attributes?.gravity = Gravity.BOTTOM
        window?.attributes = attributes
    }

    private inner class BottomRecyclerAdapter : RecyclerView.Adapter<BottomRecyclerAdapter.BottomRecyclerViewHolder>() {
        var selectorDatas: MutableList<String> = mutableListOf()
            set(value) {
                field = value
                notifyDataSetChanged()
            }

        var selectorPosition: Int = 0
            set(value) {
                field = value
                notifyDataSetChanged()
            }

        private var onItemClickListener: ((adapter: RecyclerView.Adapter<*>, view: View?, position: Int) -> Unit)? = null

        fun setOnItemClickListener(listener: (adapter: RecyclerView.Adapter<*>, view: View?, position: Int) -> Unit) {
            onItemClickListener = listener
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BottomRecyclerAdapter.BottomRecyclerViewHolder {
            val itemView = LayoutInflater.from(parent.context)
                    .inflate(R.layout.view_bottom_selector_content_item_layout, parent, false)
            return BottomRecyclerViewHolder(itemView)
        }

        override fun onBindViewHolder(holder: BottomRecyclerAdapter.BottomRecyclerViewHolder, position: Int) {
            val item = selectorDatas[position]
            val tvShow = holder.itemView.bottom_selector_item_tv_show
            tvShow.text = item

            if (selectorPosition == position) {
                holder.itemView.setBackgroundResource(R.color.color_f4f8fe)
            } else {
                holder.itemView.setBackgroundResource(R.color.white)
            }

            tvShow.setOnClickListener {
                onItemClickListener?.invoke(this@BottomRecyclerAdapter, it, position)
            }
        }

        override fun getItemCount(): Int {
            return selectorDatas.size
        }

        inner class BottomRecyclerViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView)
    }
}